package com.liu.dao.impl;

import com.liu.dao.INovelDao;
import com.liu.pojo.Novel;
import com.liu.util.LoggerFactory;

import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 老刘Matthew
 * @Timestamp 2022/7/13 23:10
 * @Description
 */
public class NovelDaoImpl extends GenericDaoImpl<Novel> implements INovelDao, LoggerFactory {

    @Override
    public List<Novel> queryListByUserid(Integer user_id) throws SQLException {

        return query(" where user_id = " + user_id);
    }

    //小说信息查询
    @Override
    public List<Novel> queryListByAuthor_name(String author_name) throws SQLException {

        String sql = //
                "select " +
                    "cate_name, " +
                    "nsize, " +
                    "down_type, " +
                    "progress, " +
                    "user_name, " +
                    "push_time, " +
                    "down_count, " +
                    "read_count, " +
                    "recommend, " +
                    "`user`.user_id, " +
                    "ntype, " +
                    "author_name, " +
                    "novel_id " +
                "from novel " +
                "join category " +
                "on novel.cate_id = category.sort " +
                "join `user` on novel.user_id = `user`.user_id " +
                "where novel.author_name = '" + author_name + "'";

        return jdbcTemplate.query(sql, resultSet -> {

            List<Novel> list = new ArrayList<>();

            while (resultSet.next()) {

                Novel o = new Novel();

                o.setContent(resultSet.getString("cate_name")); //用content接
                o.setNsize(resultSet.getDouble("nsize"));
                o.setDown_type(resultSet.getInt("down_type"));
                o.setProgress(resultSet.getInt("progress"));
                o.setPush_time(resultSet.getTimestamp("push_time"));
                o.setDown_count(resultSet.getInt("down_count"));
                o.setRead_count(resultSet.getInt("read_count"));
                o.setRecommend(resultSet.getInt("recommend"));
                o.setUser_id(resultSet.getInt("user_id"));
                o.setAuthor_name(resultSet.getString("author_name"));
                o.setTitle(resultSet.getString("user_name")); //接作者名
                o.setNtype(resultSet.getInt("ntype"));
                o.setNovel_id(resultSet.getInt("novel_id"));

                list.add(o);
            }

            return list;
        });
    }


    @Override
    public List<Novel> queryListByKeyword(String keyword) throws SQLException {

        String sql = //
                "select " +
                    "novel_id, " +
                    "author_name, " +
                    "push_time, " +
                    "down_count, " +
                    "read_count, " +
                    "recommend, " +
                    "state " +
                "from novel " +
                "where author_name " +
                "like '%" + keyword + "%' ";

        return getListNovel(sql);
    }

    @Override
    public Integer save(Novel o) throws SQLException {

        String sql = //
                "insert into " +
                        NOVEL + " (" +
                        "user_id, " +
                        "title, " +
                        "author_name, " +
                        "cate_id, " +
                        "ntype, " +
                        "down_type, " +
                        "progress, " +
                        "nsize, " +
                        "push_time, " +
                        "last_update, " +
                        "content, " +
                        "down_count, " +
                        "read_count, " +
                        "recommend, " +
                        "state, " +
                        "is_recommend " +
                        ") " +
                        "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        return jdbcTemplate.updateOrInsert(sql,
                new Object[]{
                        o.getUser_id(),
                        o.getTitle(),
                        o.getAuthor_name(),
                        o.getCate_id(),
                        o.getNtype(),
                        o.getDown_type(),
                        o.getProgress(),
                        o.getNsize(),
                        o.getPush_time(),
                        o.getLast_update(),
                        o.getContent(),
                        o.getDown_count(),
                        o.getRead_count(),
                        o.getRecommend(),
                        o.getState(),
                        o.getIs_recommend()
                },
                new Integer[]{
                        Types.INTEGER,  //
                        Types.VARCHAR,  //
                        Types.VARCHAR,  //
                        Types.INTEGER,  //
                        Types.INTEGER,  //
                        Types.INTEGER,  //
                        Types.INTEGER,  //
                        Types.DOUBLE,   //
                        Types.VARCHAR,  //
                        Types.VARCHAR,  //
                        Types.VARCHAR,  //
                        Types.INTEGER,  //
                        Types.INTEGER,  //
                        Types.INTEGER,  //
                        Types.INTEGER,  //
                        Types.INTEGER   //
                });
    }

    @Override
    public List<Novel> query(String where) throws SQLException {

        String sql =
                "select " +
                        "novel_id," +
                        "author_name, " +
                        "push_time, " +
                        "down_count, " +
                        "read_count, " +
                        "recommend, " +
                        "state " +
                        "from " + NOVEL +
                        appendWhere(where);

        return getListNovel(sql);
    }

    private List<Novel> getListNovel(String sql) throws SQLException {

        return jdbcTemplate.query(sql, resultSet -> {

            List<Novel> list = new ArrayList<>();

            while (resultSet.next()) {

                Novel o = new Novel();
                o.setNovel_id(resultSet.getInt("novel_id"));
                o.setAuthor_name(resultSet.getString("author_name"));
                o.setPush_time(resultSet.getTimestamp("push_time"));
                o.setDown_count(resultSet.getInt("down_count"));
                o.setRead_count(resultSet.getInt("read_count"));
                o.setRecommend(resultSet.getInt("recommend"));
                o.setState(resultSet.getInt("state"));

                list.add(o);
            }

            return list;
        });
    }

    @Override
    public List<Novel> queryAll() throws SQLException {

        return null;
    }

    @Override
    public Novel querySingle(Integer id) throws SQLException {
        return null;
    }

    @Override
    public Integer queryCount(String table, String where) throws SQLException {
        return null;
    }

    @Override
    public Integer update(Novel novel) throws SQLException {
        return null;
    }

}
